About MVC support please

kadir 60 Reputation points
2024-07-19T17:44:19.46+00:00
     <div class="row" id="education-seminar-forms-BilgisayarBilgi">

                                                @for (int i = 0; i < Model.tBL_BilgisayarBilgisis.Count; i++)

                                                {

                                                    <div class="col-xl-6 mb-6">

                                                        <div class="input-block mb-6">

                                                            <label class="col-form-label">Program Adı</label>

                                                            @Html.EditorFor(model => model.tBL_BilgisayarBilgisis[i].ProgramAdi, new { htmlAttributes = new { @class = "form-control", @Value = "MS Office (Excel)", @readonly = "readonly" } })

                                                            @Html.ValidationMessageFor(model => model.tBL_BilgisayarBilgisis[i].ProgramAdi, "", new { @class = "text-danger" })

                                                        </div>

                                                    </div>

                                                    <div class="col-xl-6 mb-6">

                                                        <div class="input-block mb-6">

                                                            <label class="col-form-label">Seviye</label>

                                                            @Html.DropDownListFor(model => model.tBL_BilgisayarBilgisis[i].seviye, new List<SelectListItem>

                                                       {

                                                           new SelectListItem { Text = "Orta", Value = "Orta" },

                                                           new SelectListItem { Text = "İyi", Value = "İyi" },

                                                           new SelectListItem { Text = "İleri", Value = "İleri" }

                                                       }, "Seçiniz", new { @class = "form-select" })

                                                            @Html.ValidationMessageFor(model => model.tBL_BilgisayarBilgisis[i].seviye, "", new { @class = "text-danger" })

                                                        </div>

                                                    </div>


                                              

                                                    <div class="col-xl-6 mb-6">

                                                        <div class="input-block mb-6">

                                                            <label class="col-form-label">Program Adı</label>

                                                            @Html.EditorFor(model => model.tBL_BilgisayarBilgisis[i].ProgramAdi, new { htmlAttributes = new { @class = "form-control", @Value = "MS Office (Word)", @readonly = "readonly" } })

                                                            @Html.ValidationMessageFor(model => model.tBL_BilgisayarBilgisis[i].ProgramAdi, "", new { @class = "text-danger" })

                                                        </div>

                                                    </div>

                                                    <div class="col-xl-6 mb-6">

                                                        <div class="input-block mb-6">

                                                            <label class="col-form-label">Seviye</label>

                                                            @Html.DropDownListFor(model => model.tBL_BilgisayarBilgisis[i].seviye, new List<SelectListItem>

                                                       {

                                                           new SelectListItem { Text = "Orta", Value = "Orta" },

                                                           new SelectListItem { Text = "İyi", Value = "İyi" },

                                                           new SelectListItem { Text = "İleri", Value = "İleri" }

                                                       }, "Seçiniz", new { @class = "form-select" })

                                                            @Html.ValidationMessageFor(model => model.tBL_BilgisayarBilgisis[i].seviye, "", new { @class = "text-danger" })

                                                        </div>

                                                    </div>

                                                    <div class="col-xl-6 mb-6">

                                                        <div class="input-block mb-6">

                                                            <label class="col-form-label">Program Adı</label>

                                                            @Html.EditorFor(model => model.tBL_BilgisayarBilgisis[i].ProgramAdi, new { htmlAttributes = new { @class = "form-control", @Value = "MS Office (Powerpoint)", @readonly = "readonly" } })

                                                            @Html.ValidationMessageFor(model => model.tBL_BilgisayarBilgisis[i].ProgramAdi, "", new { @class = "text-danger" })

                                                        </div>

                                                    </div>

                                                    <div class="col-xl-6 mb-6">

                                                        <div class="input-block mb-6">

                                                            <label class="col-form-label">Seviye</label>

                                                            @Html.DropDownListFor(model => model.tBL_BilgisayarBilgisis[i].seviye, new List<SelectListItem>

                                                       {

                                                           new SelectListItem { Text = "Orta", Value = "Orta" },

                                                           new SelectListItem { Text = "İyi", Value = "İyi" },

                                                           new SelectListItem { Text = "İleri", Value = "İleri" }

                                                       }, "Seçiniz", new { @class = "form-select" })

                                                            @Html.ValidationMessageFor(model => model.tBL_BilgisayarBilgisis[i].seviye, "", new { @class = "text-danger" })

                                                        </div>

                                                    </div>

                                                    <div class="col-xl-6 mb-6">

                                                        <div class="input-block mb-6">

                                                            <label class="col-form-label">Program Adı</label>

                                                            @Html.EditorFor(model => model.tBL_BilgisayarBilgisis[i].ProgramAdi, new { htmlAttributes = new { @class = "form-control", @Value = "MS Office (Outlook)", @readonly = "readonly" } })

                                                            @Html.ValidationMessageFor(model => model.tBL_BilgisayarBilgisis[i].ProgramAdi, "", new { @class = "text-danger" })

                                                        </div>

                                                    </div>

                                                    <div class="col-xl-6 mb-6">

                                                        <div class="input-block mb-6">

                                                            <label class="col-form-label">Seviye</label>

                                                            @Html.DropDownListFor(model => model.tBL_BilgisayarBilgisis[i].seviye, new List<SelectListItem>

                                                       {

                                                           new SelectListItem { Text = "Orta", Value = "Orta" },

                                                           new SelectListItem { Text = "İyi", Value = "İyi" },

                                                           new SelectListItem { Text = "İleri", Value = "İleri" }

                                                       }, "Seçiniz", new { @class = "form-select" })

                                                            @Html.ValidationMessageFor(model => model.tBL_BilgisayarBilgisis[i].seviye, "", new { @class = "text-danger" })

                                                        </div>

                                                    </div>

                                                }

                                                <div class="row">

                                                    <div class="col-md-12">

                                                        <div class="text-end">

                                                            <button type="button" class="btn btn-primary" id="add-more-BilgisayarBilgi" style=" align-items: flex-start; display: block; padding: 10px !important; ">Bilgisayar bilgisi Ekle</button>

                                                        </div>

                                                    </div>

                                                </div>

                                                <script>

                                                    $(document).ready(function () {

                                                        var maxFields = 55;

                                                        var addButton = $('#add-more-BilgisayarBilgi');

                                                        var wrapper = $('#education-seminar-forms-BilgisayarBilgi'); // Container for education sections

                                                        var formCount = 4;

                                                        $(addButton).click(function () {

                                                            if (formCount < maxFields) {

                                                                formCount++; // Increase the form count

                                                                var newEducationSection = `

           <div class="row BilgisayarBilgi-section">

                <div class="col-xl-6 mb-6">

                    <div class="input-block mb-6">

                        <label class="col-form-label">Program Adı</label>

                        <input type="text" name="tBL_BilgisayarBilgisis[${formCount - 1}].ProgramAdi" class="form-control" />

                    </div>

                </div>

                <div class="col-xl-5 mb-5">

                    <div class="input-block mb-6">

                        <label class="col-form-label">Seviye</label>

                        <select class="form-select" name="tBL_BilgisayarBilgisis[${formCount - 1}].Seviye">

                            <option value="Orta">Orta</option>

                            <option value="İyi">İyi</option>

                            <option value="İleri">İleri</option>

                        </select>

                        <span class="text-danger field-validation-valid" data-valmsg-for="tBL_BilgisayarBilgisis[${formCount - 1}].Seviye" data-valmsg-replace="true"></span>

                    </div>

                </div>

                <div class="col-xl-1 text-end align-self-center">

                    <button type="button" class="btn btn-danger remove-button" style=" align-items: flex-start; display: block; padding: 10px !important;">X</button>

                </div>

            </div>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        `;

                                                                $(newEducationSection).insertBefore(addButton.parent());

                                                            }

                                                        });

                                                        // Handle the click event of the remove button

                                                        $(wrapper).on('click', '.remove-button', function () {

                                                            $(this).closest('.BilgisayarBilgi-section').remove();

                                                            formCount--; // Decrease the form count

                                                        });

                                                    });

                                                </script>

                                            </div>
 for (int i = 0; i < csbasvurularr.tBL_BilgisayarBilgisis.Count; i++)

            {

                var ProgramAdi = csbasvurularr.tBL_BilgisayarBilgisis[i].ProgramAdi;

                var Seviye = csbasvurularr.tBL_BilgisayarBilgisis[i].seviye;

                if (!string.IsNullOrEmpty(ProgramAdi) && !string.IsNullOrEmpty(Seviye))

                {

                    TBL_BilgisayarBilgisi tBL_BilgisayarBilgisi = new TBL_BilgisayarBilgisi();

                    tBL_BilgisayarBilgisi.GenelBilgiid = newlyCreatedId;

                    tBL_BilgisayarBilgisi.ProgramAdi = ProgramAdi;

                    tBL_BilgisayarBilgisi.seviye = Seviye;

                    tBL_BilgisayarBilgisi.isDelete = false;

                    db.TBL_BilgisayarBilgisi.Add(tBL_BilgisayarBilgisi);

                }

            }

Hello everyone, I have a problem, I ask for your help, there are 4 fixed data in for here, the user is this

has to fill in the data, but the user can also add it with the script himself, now the error starts from here when saving to the database only for

It saves the first data in the loop and what the user adds with the script, that is, it passes the other 3 data in the for, I wonder how to solve this problem.

how can I make the user save the first 4 data fixed so I can solve it

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,507 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 29,751 Reputation points Microsoft Vendor
    2024-07-22T07:23:53.1566667+00:00

    Hi @kadir,

    I think there is something wrong with your design.

    According to your description, there are four fixed data on the view, which means your database has only one data. @for (int i = 0; i < Model.tBL_BilgisayarBilgisis.Count; i++) only loops once.

    The HTML of your four fixed data have the same id and name. You create invalid HTML due to the duplicate id attribute.So only one piece of data will be returned.

    User's image

    If you use model binding, you should set List<ProgramAdi> and List<seviye>.

    but still... You can get the posted values as an array by calling Request.Form. For example:

    string[] amounts = Request.Form["tBL_BilgisayarBilgisis[1].ProgramAdi"].Split(new char[] { ',' });

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.